#!/usr/bin/env bash

ROOT=~/data/YLIMED/mp4_short_encode
TrainSplit=$ROOT/YLIMED_short_mp4_EvAll_split_train.txt
TestSplit=$ROOT/YLIMED_short_mp4_EvAll_split_test.txt
ValSplit=$ROOT/YLIMED_short_mp4_EvAll_split_val.txt
PosValSplit=$ROOT/YLIMED_short_mp4_Ev101_110_split_val.txt
DEVTrainSplit=$ROOT/YLIMED_short_mp4_EvAll_split_train_dev.txt
DEVTestSplit=$ROOT/YLIMED_short_mp4_EvAll_split_test_dev.txt
TOYSplit=$ROOT/YLIMED_short_mp4_EvAll_split_test_toy.txt

# coviar_lstm00  coviar_lstmlrx10  coviar_freezeCNN_lstm
# coviar_freezeCNN_lstmlrx10  mv_res_resnet50  freeze_cnn_lstm_seg5
# coviar_cnnlrx10_lstm  cnn_lstm_seg5
NAME=$1
LOG=./out/$NAME/log
MODEL=./out/$NAME/weights
CoViAR_MODEL=../pytorch-coviar/out/ylimed_finetuning01/weights

TEST_OUT=./out/$NAME/scores
TSNE=./out/$NAME/tSNE


if [ $NAME == "dev" ]
then
    echo "develop"
    # CUDA_VISIBLE_DEVICES=0,1,2,3 \
    # python train.py --arch resnet152 --data-name ylimed \
    #     --data-root  $ROOT \
    #     --train-list $TOYSplit \
    #     --test-list  $TOYSplit \
    #     --representation iframe \
    #     --model-prefix $MODEL/ylimed \
    #     --lr      0.0003 \
    #     --lstm_lr 0.0003 \
    #     --batch-size 4 \
    #     --lr-steps 40 80 100 \
    #     --epochs 120 \
    #     --gpus 0
    # exit

    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python train.py --lr 0.005 --batch-size 4 --arch resnet50 \
        --data-name ylimed --representation mv \
        --data-root $ROOT \
        --train-list $TOYSplit \
        --test-list $TOYSplit  \
        --model-prefix ylimed_toy \
        --lr-steps 80 150 200 \
        --epochs 230 \
        --gpus 0 \
        # --freeze \
        # --weights $CoViAR_MODEL/ylimed_mv_model_best.pth.tar
        # > $LOG/ylimed_mv_model.out 2>&1
    set +x
    exit

elif [ $NAME == "04_coviar_cnnlrx10_lstm" ]
then
    # CUDA_VISIBLE_DEVICES=0,1,2,3 \
    # python train.py --arch resnet152 --data-name ylimed \
    #     --data-root  $ROOT \
    #     --train-list $TrainSplit \
    #     --test-list  $PosValSplit \
    #     --representation iframe \
    #     --model-prefix $MODEL/ylimed \
    #     --lr      0.003 \
    #     --lstm_lr 0.0003 \
    #     --batch-size 32 \
    #     --lr-steps 40 80 100 \
    #     --epochs 120 \
    #     --num_segments 3 \
    #     --gpus 0 1 > $LOG/ylimed_iframe_model.out 2>&1 &
    #
    # CUDA_VISIBLE_DEVICES=2,3 \
    # python train.py --arch resnet18 --data-name ylimed \
    #     --data-root  $ROOT \
    #     --train-list $TrainSplit \
    #     --test-list  $PosValSplit \
    #     --representation mv \
    #     --model-prefix $MODEL/ylimed \
    #     --lr      0.05 \
    #     --lstm_lr 0.005 \
    #     --batch-size 80 \
    #     --lr-steps 80 150 200 \
    #     --epochs 230 \
    #     --num_segments 3 \
    #     --gpus 0 > $LOG/ylimed_mv_model.out 2>&1 &

    CUDA_VISIBLE_DEVICES=3 \
    python train.py --arch resnet18 --data-name ylimed \
        --data-root  $ROOT \
        --train-list $TrainSplit \
        --test-list  $PosValSplit \
        --representation residual \
        --model-prefix $MODEL/ylimed \
        --lr      0.01 \
        --lstm_lr 0.001 \
        --batch-size 80 \
        --lr-steps 80 120 180 \
        --epochs 200 \
        --num_segments 3 \
        --gpus 0 > $LOG/ylimed_residual_model.out 2>&1 &

    wait

    # CUDA_VISIBLE_DEVICES=0,1,2,3 \
    # python test.py \
    #     --gpus 0 \
    #     --arch resnet152 --data-name ylimed --representation iframe \
    #     --data-root $ROOT \
    #     --test-list $TestSplit \
    #     --weights $MODEL/ylimed_iframe_model_best.pth.tar \
    #     --save-scores $TEST_OUT/ylimed_best_iframe_model__scores \
    #     > $TEST_OUT/ylimed_best_iframe_model__scores.out 2>&1 &
    #
    # CUDA_VISIBLE_DEVICES=0,1,2,3 \
    # python test.py \
    #     --gpus 0 \
    #     --arch resnet18 --data-name ylimed --representation mv \
    #     --data-root $ROOT \
    #     --test-list $TestSplit \
    #     --weights $MODEL/ylimed_mv_model_best.pth.tar \
    #     --save-scores $TEST_OUT/ylimed_best_mv_model__scores \
    #     > $TEST_OUT/ylimed_best_mv_model__scores.out 2>&1 &

    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python test.py \
        --gpus 3 \
        --arch resnet18 --data-name ylimed --representation residual \
        --data-root $ROOT \
        --test-list $TestSplit \
        --weights $MODEL/ylimed_residual_model_best.pth.tar \
        --save-scores $TEST_OUT/ylimed_best_residual_model__scores \
        > $TEST_OUT/ylimed_best_residual_model__scores.out 2>&1 &

elif [ $NAME == "cnn-lstmlrx10" ]
then
    echo "cnn-lstmlrx10 ylimed I S: $(date "+%Y-%m-%d %H:%M:%S")"
    set -x
    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python train.py --arch resnet152 --data-name ylimed \
        --data-root  $ROOT \
        --train-list $TrainSplit \
        --test-list  $PosValSplit \
        --representation iframe \
        --model-prefix $MODEL/ylimed \
        --lr      0.0003 \
        --lstm_lr 0.003 \
        --batch-size 32 \
        --lr-steps 40 80 100 \
        --epochs 120 \
        --weights $CoViAR_MODEL/ylimed_iframe_model_best.pth.tar \
        --gpus 0 1 > $LOG/ylimed_iframe_model.out 2>&1 &
    set +x

elif [ $NAME == "lstm-resnet50" ]
then
    set -x
    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python train.py --arch resnet50 --data-name ylimed \
        --data-root  $ROOT \
        --train-list $TrainSplit \
        --test-list  $PosValSplit \
        --representation mv \
        --model-prefix $MODEL/ylimed \
        --lr      0.005 \
        --lstm_lr 0.005 \
        --batch-size 64 \
        --lr-steps 80 150 200 \
        --epochs 230 \
        --gpus 0 1 > $LOG/ylimed_mv_model.out 2>&1 &
    # Note: 1和>之间需要空格

    CUDA_VISIBLE_DEVICES=2,3 \
    python train.py --arch resnet50 --data-name ylimed \
        --data-root  $ROOT \
        --train-list $TrainSplit \
        --test-list  $PosValSplit \
        --representation residual \
        --model-prefix $MODEL/ylimed \
        --lr      0.001 \
        --lstm_lr 0.001 \
        --batch-size 64 \
        --lr-steps 80 120 180 \
        --epochs 200 \
        --gpus 0 1 > $LOG/ylimed_residual_model.out 2>&1 &

    wait

    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python test.py \
        --gpus 0 \
        --arch resnet50 --data-name ylimed --representation mv \
        --data-root $ROOT \
        --test-list $TestSplit \
        --weights $MODEL/ylimed_mv_model_best.pth.tar \
        --save-scores $SCORE/ylimed_best_mv_model__scores \
        > $OUT/ylimed_best_mv_model__scores.out 2>&1 &

    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python test.py \
        --gpus 0 \
        --arch resnet50 --data-name ylimed --representation residual \
        --data-root $ROOT \
        --test-list $TestSplit \
        --weights $MODEL/ylimed_residual_model_best.pth.tar \
        --save-scores $SCORE/ylimed_best_residual_model__scores \
        > $OUT/ylimed_best_residual_model__scores.out 2>&1 &
    set +x
elif [ $NAME == "cnn_lstm_seg5" ]
then
    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python train.py --arch resnet152 --data-name ylimed \
        --data-root  $ROOT \
        --train-list $TrainSplit \
        --test-list  $PosValSplit \
        --representation iframe \
        --model-prefix $MODEL/ylimed \
        --lr      0.0003 \
        --lstm_lr 0.0003 \
        --batch-size 25 \
        --lr-steps 40 80 100 \
        --epochs 120 \
        --num_segments 5 \
        --gpus 0 1 2 > $LOG/ylimed_iframe_model.out 2>&1 &

    CUDA_VISIBLE_DEVICES=3 \
    python train.py --arch resnet18 --data-name ylimed \
        --data-root  $ROOT \
        --train-list $TrainSplit \
        --test-list  $PosValSplit \
        --representation mv \
        --model-prefix $MODEL/ylimed \
        --lr      0.005 \
        --lstm_lr 0.005 \
        --batch-size 64 \
        --lr-steps 80 150 200 \
        --epochs 230 \
        --num_segments 5 \
        --gpus 0 > $LOG/ylimed_mv_model.out 2>&1 &
    wait

    CUDA_VISIBLE_DEVICES=3 \
    python train.py --arch resnet18 --data-name ylimed \
        --data-root  $ROOT \
        --train-list $TrainSplit \
        --test-list  $PosValSplit \
        --representation residual \
        --model-prefix $MODEL/ylimed \
        --lr      0.001 \
        --lstm_lr 0.001 \
        --batch-size 64 \
        --lr-steps 80 120 180 \
        --epochs 200 \
        --num_segments 5 \
        --gpus 0 > $LOG/ylimed_residual_model.out 2>&1 &


    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python test.py \
        --gpus 0 \
        --arch resnet152 --data-name ylimed --representation iframe \
        --data-root $ROOT \
        --test-list $TestSplit \
        --weights $MODEL/ylimed_iframe_model_best.pth.tar \
        --save-scores $TEST_OUT/ylimed_best_iframe_model__scores \
        > $TEST_OUT/ylimed_best_iframe_model__scores.out 2>&1 &

    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python test.py \
        --gpus 0 \
        --arch resnet18 --data-name ylimed --representation mv \
        --data-root $ROOT \
        --test-list $TestSplit \
        --weights $MODEL/ylimed_mv_model_best.pth.tar \
        --save-scores $TEST_OUT/ylimed_best_mv_model__scores \
        > $TEST_OUT/ylimed_best_mv_model__scores.out 2>&1 &

    wait
    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python test.py \
        --gpus 0 \
        --arch resnet18 --data-name ylimed --representation residual \
        --data-root $ROOT \
        --test-list $TestSplit \
        --weights $MODEL/ylimed_residual_model_best.pth.tar \
        --save-scores $TEST_OUT/ylimed_best_residual_model__scores \
        > $TEST_OUT/ylimed_best_residual_model__scores.out 2>&1 &

elif [ $NAME == "freeze_cnn-lstm-seg5" ]
then
    set -x
    CUDA_VISIBLE_DEVICES=0,1,2,3 \
    python train.py --arch resnet152 --data-name ylimed \
        --data-root  $ROOT \
        --train-list $TrainSplit \
        --test-list  $PosValSplit \
        --representation iframe \
        --model-prefix $MODEL/ylimed \
        --lr      0.0003 \
        --lstm_lr 0.0003 \
        --batch-size 32 \
        --lr-steps 40 80 100 \
        --epochs 120 \
        --num_segments 5 \
        --freeze \
        --weights $CoViAR_MODEL/ylimed_iframe_model_best.pth.tar \
        --gpus 0 1 > $LOG/ylimed_iframe_model.out 2>&1 &

    CUDA_VISIBLE_DEVICES=2,3 \
    python train.py --arch resnet18 --data-name ylimed \
        --data-root  $ROOT \
        --train-list $TrainSplit \
        --test-list  $PosValSplit \
        --representation mv \
        --model-prefix $MODEL/ylimed \
        --lr      0.005 \
        --lstm_lr 0.005 \
        --batch-size 80 \
        --lr-steps 80 150 200 \
        --epochs 230 \
        --num_segments 5 \
        --freeze \
        --weights $CoViAR_MODEL/ylimed_mv_model_best.pth.tar \
        --gpus 0 > $LOG/ylimed_mv_model.out 2>&1 &

    CUDA_VISIBLE_DEVICES=3 \
    python train.py --arch resnet18 --data-name ylimed \
        --data-root  $ROOT \
        --train-list $TrainSplit \
        --test-list  $PosValSplit \
        --representation residual \
        --model-prefix $MODEL/ylimed \
        --lr      0.001 \
        --lstm_lr 0.001 \
        --batch-size 80 \
        --lr-steps 80 120 180 \
        --epochs 200 \
        --num_segments 5 \
        --freeze \
        --weights $CoViAR_MODEL/ylimed_residual_model_best.pth.tar \
        --gpus 0 > $LOG/ylimed_residual_model.out 2>&1 &
    set +x

elif [ $NAME == "freeze_cnn-lstm" ]
then
    echo "freeze_cnn-lstm ylimed I S: $(date "+%Y-%m-%d %H:%M:%S")"
else
    echo "Please make sure the positon variable is ..."
fi
